﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Obj
    {
        int x;
    }
    class A
    {
            public A()
            {
                PrintFields();
            }

            public virtual void PrintFields()
            {
            }

    }

    class B : A
    {
        int x = 1;
        int y;
        public B()
        {
            y = -1;
        }
        public override void PrintFields()
        {
            Console.WriteLine("x={0},y={1}", x, y);
        }
    }

    public class A1
    {
        public void Print()
        {
            Console.WriteLine("A");
        }

        public A1()
        {
            Print();
        }
    }

    public class B1 : A1
    {
        public new void Print()
        {
            Console.WriteLine("B");
        }

        public B1()
        {
            Print();
        }
    }

    public class A2
    {
        public void Print()
        {
            Console.WriteLine("A");
        }

        public A2()
        {
            Print();
        }
    }

    public class B2 : A2
    {
        public new void Print()
        {
            Console.WriteLine("B");
        }

        public B2()
        {
            Print();
        }

    }


    public class A3
    {
        public virtual void Print()
        {
            Console.WriteLine("A");
        }

        public A3()
        {
            Print();
        }
    }

    public class B3 : A3
    {
        public override void Print()
        {
            Console.WriteLine("B");
        }

        public B3()
        {
            Print();
        }
    }

    class Program
    {

        static int test()
        {
            int i = 0;
            try
            {
                return i++;
            }
            finally
            {
                i--;
            }
        }

        static Boolean Equals()
        {
            Obj obj = new Obj();
            Obj obj1 = new Obj();
            //return obj.Equals(obj1);//fasle
            return obj.GetHashCode() == obj1.GetHashCode();//fasle

            //var x = 1;
            //var y = 1;
            //return x.Equals(y);
            List<int> o = new List<int>();
        }


        static void Main(string[] args)
        {
            //B b = new B();
            //b.PrintFields();

            //B1 b1 = new B1();
            //b1.Print();

            //A2 b2 = new B2();
            //b2.Print();
            //B2 b22 = new B2();
            //b22.Print();

            //B2 actualB = b2 as B2; actualB.Print();

            //B3 b3 = new B3();
            //b3.Print();
            //A3 b33 = new B3();
            //b33.Print();

            //A3 actualBb = b3 as A3; actualBb.Print();
            //var i = test();
            //Console.WriteLine(i);

            //var b = Equals();
            //Console.WriteLine(b);
            //short i = 1;
            //switch (i)
            //{
            //    case 1:
            //        break;
            //    case 2:
            //        var oo = 0;
            //        break;
            //}
            ISendMessage message = new ShanXiMessage();
            var result = message.Send("测试 - 内容", "15914339702");
            message = new JiangXiShengWeiMessage();
            result = message.Send("测试 - 内容省委", "15914339702");
            if (result)
            {
                Console.WriteLine("成功！");
            }
            else
            {
                Console.WriteLine("失败！"); 
            }


            Console.Read();

        }
    }

    /// <summary>
    /// 短信发送接口
    /// </summary>
    public interface ISendMessage 
    {
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="content">短信内容</param>
        /// <param name="phones">电话号码，逗号隔开</param>
        /// <returns>True:成功；False:失败</returns>
        bool Send(string content,string phones);
    }

    /// <summary>
    /// 验证短信逻辑
    /// </summary>
    public class CheckMessage
    {
        /// <summary>
        /// 验证发送内容的长度，空也排除
        /// </summary>
        /// <param name="content">短信内容</param>
        /// <returns>True:超出指定长度；False:未超出</returns>
        public virtual bool CheckLength(string content)
        {
            return false;
        }

        /// <summary>
        /// 验证发送内容的,是否符合指定的规则
        /// </summary>
        /// <param name="content">短信内容</param>
        /// <returns>True:符合规则可以发送；False:不符合规则不能发送</returns>
        public virtual bool CheckContent(string content)
        {
            return true;
        }

        /// <summary>
        /// 验证发送的电话号码，正则验证，并支持多个，踢出最后一个逗号
        /// </summary>
        /// <param name="phones">电话号码，逗号隔开</param>
        /// <returns>True:符合电话号码规则可以发送；False:不符合规则不能发送</returns>
        public virtual bool CheckPhones(string phones)
        {
            return true;
        }
    }

    /// <summary>
    /// 验证发送短信次数
    /// </summary>
    public class CheckSentCount
    { 
        
    }

    /// <summary>
    /// 验证发送短信时间
    /// </summary>
    public class CheckSentTime 
    {
        
    }
    
    /// <summary>
    /// Http Post发送短信至相关网关
    /// </summary>
    public abstract class HttpMessage : CheckMessage, ISendMessage
    {

        public bool Send(string content, string phones)
        {
            if (CheckLength(content))
            {
                return false;
            }
            if (!CheckContent(content))
            {
                return false;
            }
            if (!CheckPhones(phones))
            {
                return false;
            }
            return Post(content,phones);
        }

        /// <summary>
        /// Post 数据至Http接口
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="phones">电话号码</param>
        /// <returns></returns>
        public abstract bool Post(string content, string phones);
    }

    /// <summary>
    /// 写入数据库以便第三方服务发送
    /// </summary>
    public abstract class DataBaseMessage :CheckMessage, ISendMessage
    {
        /// <summary>
        /// 数据库链接字符串
        /// </summary>
        public string ConnectStr { get; set; }

        public virtual bool CheckLength(string content)
        {
            return true;
        }

        public virtual bool CheckContent(string content)
        {
            return true;
        }

        public virtual bool CheckPhones(string phones)
        {
            return true;
        }

        public bool Send(string content, string phones)
        {
            if (!CheckLength(content))
            {
                return false;
            }
            if (!CheckContent(content))
            {
                return false;
            }
            if (!CheckPhones(phones))
            {
                return false;
            }
            return Add(content, phones);
        }

        /// <summary>
        /// 写入数据库
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="phones">电话号码</param>
        /// <returns></returns>
        public abstract bool Add(string content, string phones);
    }

    /// <summary>
    /// 陕西数据库短信接口
    /// </summary>
    public class ShanXiMessage : DataBaseMessage
    {

        public override bool Add(string content, string phones)
        {
            //使用陕西数据库字符串写入库
            return true;
        }
    }

    /// <summary>
    /// 上海数据库短信接口
    /// </summary>
    public class ShangHaiMessage : DataBaseMessage
    {
        /// <summary>
        /// 上海数据库短信接口需要验证发送内容长度，最多不能超过10个字符
        /// </summary>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public override bool CheckLength(string content)
        {
            return string.IsNullOrEmpty(content) ? false : (content.Length > 10);
        }

        public override bool Add(string content, string phones)
        {
            //使用陕西数据库字符串写入库
            return true;
        }
    }

    /// <summary>
    /// 中石油短信接口
    /// </summary>
    public class ZhongShiYouMessage : HttpMessage
    {

        public override bool Post(string content, string phones)
        {
            //模拟Http Post方式发送至相关网关
            return true;
        }
    }

    /// <summary>
    /// 江西省委短信接口
    /// </summary>
    public class JiangXiShengWeiMessage : HttpMessage
    {
        /// <summary>
        /// 验证是否包含非法字符
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public override bool CheckContent(string content)
        {
            return !content.Contains("省委");
        }

        public override bool Post(string content, string phones)
        {
            //模拟发送
            return true;
        }
    }
}
